package com.martinwj.controller.collection;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.martinwj.constant.ErrorMsg;
import com.martinwj.entity.Collection;
import com.martinwj.entity.Result;
import com.martinwj.entity.User;
import com.martinwj.exception.SysException;
import com.martinwj.service.CollectionService;
import com.martinwj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CollectionAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2021-04-05
 */
@Controller
@RequestMapping("portal/collection_info")
public class CollectionAction {
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private UserService userService;

    /**
     * 判断当前视频是否已被用户收藏过了
     * @throws SysException
     */
    @RequestMapping("isCollectioned_by_user.json")
    @ResponseBody
    public Result isCollectionedByUser(
            @RequestParam(value="mediaId") String mediaId,
            @RequestParam(value="userId") String userId) throws SysException {

        if (StringUtils.isEmpty(userId)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        int count = collectionService.countByMediaIdAndUserId(mediaId, userId);
        if (count>0) {
            throw new SysException(ErrorMsg.ERROR_X00001);
        }

        return Result.success();
    }

    /**
     * 添加视频收藏
     * @throws SysException
     */
    @RequestMapping("save.json")
    @ResponseBody
    public Result save(
            HttpServletRequest request,
            @RequestParam(value="mediaId") String mediaId,
            @RequestParam(value="userToken") String userToken) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        Collection collectionInfo = new Collection();
        collectionInfo.setMediaId(mediaId);
        collectionInfo.setUserId(userInfo.getId());

        collectionService.save(collectionInfo);

        return Result.success();
    }

    /**
     * 获取收藏列表
     * @param request
     * @param userToken 用户凭证
     * @param pageNum
     * @param pageSize
     * @return
     * @throws SysException
     */
    @RequestMapping("get_collection_list.json")
    @ResponseBody
    public Result getCollectionList(
            HttpServletRequest request,
            @RequestParam(value="userToken") String userToken,
            @RequestParam(value="pageNum", defaultValue="1") int pageNum,
            @RequestParam(value="pageSize", defaultValue="15") int pageSize) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = collectionService.listCollection(userInfo.getId());
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);

        return Result.success().add("pageInfo", pageInfo);
    }

    /**
     * 删除收藏的视频
     * @param request
     * @param mediaId 媒体主键
     * @param userToken 用户凭证
     * @return
     * @throws SysException
     */
    @RequestMapping("delete_collection.json")
    @ResponseBody
    public Result deleteCollection(
            HttpServletRequest request,
            @RequestParam(required=false, value="mediaId") String mediaId,
            @RequestParam(value="userToken") String userToken) throws SysException {

        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User user = (User) session.getAttribute("userInfo");

        if (user == null) {
            user = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", user);
        }

        Collection collection = new Collection();
        collection.setMediaId(mediaId);
        collection.setUserId(user.getId());

        collectionService.delete(collection);

        return Result.success();
    }
}
